package com.jxl.controller.admin;

import com.jxl.common.result.PageResult;
import com.jxl.common.result.Result;
import com.jxl.pojo.dto.UserPageQueryDTO;
import com.jxl.pojo.dto.UsersDTO;
import com.jxl.pojo.entity.Users;
import com.jxl.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/admin/user")
@Api(tags = "用户管理接口")
public class UserController {

    @Autowired
    private UserService usersService;

    /**
     * 根据id查看用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看用户信息")
    public Result<Users> getUsers(@PathVariable Long id){
        log.info("查看用户信息");
        Users users = usersService.getUsersById(id);
        return Result.success(users);
    }

    /**
     * 用户分页查询
     * @param
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO){
        log.info("员工分页查询，参数为:{}",userPageQueryDTO);
        PageResult pageResult = usersService.pageQueryUser(userPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增用户
     * @param usersDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增员工")
    public Result saveUser(@RequestBody UsersDTO usersDTO){
        log.info("新增员工：{}",usersDTO);
        usersService.saveUser(usersDTO);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param usersDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result updateUser(@RequestBody UsersDTO usersDTO){
        log.info("修改员工信息:{}",usersDTO);
        usersService.updateUser(usersDTO);
        return Result.success();
    }


    /**
     * 启用或禁用用户
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用用户")
    public Result setStatusUser(@PathVariable Integer status, Long id){
        log.info("启用或禁用用户:{},{}",status,id);
        usersService.setStatusUser(status,id);
        return Result.success();
    }

}
